<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class OrderCaseDescribe extends Model
{
    protected $table = "c_order_case_describes";

    public function getAllergyAttribute($value)
    {
        return json_decode($value);

    }

    public function getDiseasesHistoryAttribute($value)
    {
        return json_decode($value,true);

    }

    public function setPictureAttribute($gallery)
    {
        if (is_array($gallery)) {
            $this->attributes['picture'] = json_encode($gallery);
        }
    }

    public function getPictureAttribute($gallery)
    {
        return json_decode($gallery, true);
    }


    /**
     * 同步病例
     */
    public function setSyncCaseDescribe($order_id, $param)
    {
        $case_info = (new CaseDescribe())->where([['id', '=', $param['case_id']]])->first();
        $this->name = $case_info->name;
        $this->sex = $case_info->sex;
        $this->age = $case_info->age;
        $this->bloodType = $case_info->bloodType;
        if (is_array($case_info->allergy)) {
            $this->allergy = json_encode($case_info->allergy);
        } else {
            $this->allergy = $case_info->allergy;
        }
        if (is_array($case_info->diseases_history)) {
            $this->diseases_history = json_encode($case_info->diseases_history);
        } else {
            $this->diseases_history = $case_info->diseases_history;
        }
//        if (is_array($case_info->picture)) {
//            $this->picture = json_encode($case_info->picture);
//        } else {
//            $this->picture = $case_info->picture;
//        }
        $this->picture = isset($case_info->picture)?$case_info->picture:"";
        $this->relation = $case_info->relation;
        $this->describe = isset($case_info->describe)?$case_info->describe:"";
        $this->order_id = $order_id;
        $this->user_id = $param['user_id'];
        $this->case_id = $param['case_id'];
        return $this->save();
    }



}
